import { defineStore } from 'pinia'
import { MENU_LIST_KEY } from '@/utils/constants'

const useGlobalStore = defineStore('global', {
  state: () => ({
    privateMenuList: [],
    menuList: [],
  }),
  getters: {
    menuList() {
      if (this.privateMenuList?.length) {
        return this.privateMenuList
      }
      let localValue = localStorage.getItem(MENU_LIST_KEY)
      if (localValue) {
        localValue = JSON.parse(localValue)
      } else {
        localValue = []
      }
      return localValue
    },
  },
  actions: {
    setMenuList(menuList) {
      localStorage.setItem(MENU_LIST_KEY, JSON.stringify(menuList))
      this.privateMenuList = menuList
    },
  },
})

export default useGlobalStore
